using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using Mani.Common;

public partial class CustomerPage : BasePage
{

    public override string ActionName {
        get {
            return "vw_customer";
        }
    }

    ICustomerService _customerService;

    public ICustomerService customerService
    {
        get { return _customerService; }
        set { _customerService = value; }
    }
    static int numberDiv = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCustomerType();

        if (!Page.IsPostBack)
        {
            GetCustomers(0, true);
        }
    }

    private void GetCustomers(int pageNumber, bool setPageCount)
    {
        Customer customer = new Customer();
        customer.CustomerName = txtSearchCustomerName.Text;
        customer.IsDeleted = false;

        if (!divHiddenCustomers.Visible || rbFormalCustomers.Checked)
            customer.IsFormal = true;
        else if ( divHiddenCustomers.Visible && rbHiddenCustomers.Checked )
            customer.IsFormal = false;

        if (setPageCount)
            gvCustomers.VirtualItemCount = customerService.CountCustomers(customer);

        gvCustomers.DataSource = customerService.GetCustomers(customer, 15, pageNumber);
        gvCustomers.DataBind();
    }

    private void CheckCustomerType() {
        bool show = CommonHelper.ShowHiddenCustomers();
        gvCustomers.Columns[4].Visible = show;
        //dvCustomers.Fields[2].Visible = show;
        divHiddenCustomers.Visible = show;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetCustomers(0, true);
    }

    protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCustomers.PageIndex = e.NewPageIndex;
        GetCustomers(e.NewPageIndex, false);
    }

    protected void rbHiddenCustomers_CheckedChanged(object sender, EventArgs e)
    {
        GetCustomers(0, true);
    }

    protected void rbAll_CheckedChanged(object sender, EventArgs e)
    {
        GetCustomers(0, true);
    }

    protected void rbFormalCustomers_CheckedChanged(object sender, EventArgs e)
    {
        GetCustomers(0, true);
    }

    protected void gvCustomers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int customerId = Convert.ToInt32(gvCustomers.DataKeys[e.NewSelectedIndex].Value);
        Customer customer = customerService.FindCustomer(customerId);
        
        txtCustomerId.Text = customer.Id.ToString();
        txtCustomerName.Text = customer.CustomerName;
        cbIsFormal.Checked = (customer.IsFormal.HasValue ? customer.IsFormal.Value : true );
        txtWarningPrice.Text = (customer.WarningPrice.HasValue ? customer.WarningPrice.Value.ToString() : "");
        txtPhone.Text = customer.Telephone;
        txtAddress.Text = customer.Address;
        cbIsActive.Checked = (customer.IsActive.HasValue ? customer.IsActive.Value : true);
        txtComment.Text = customer.Comment;
        txtCreateDate.Text = (customer.CreateDate.HasValue ? MyPersianDate.DateTimeToPersian(customer.CreateDate) : MyPersianDate.DateTimeToPersian(DateTime.Now));
        txtLastUpdate.Text = (customer.LastUpdate.HasValue ? MyPersianDate.DateTimeToPersian(customer.LastUpdate) : MyPersianDate.DateTimeToPersian(DateTime.Now));

    }

    protected void btnSave_ServerClick(object sender, EventArgs e)
    {
        Customer customer = null;

        if (!String.IsNullOrEmpty(txtCustomerId.Text))
            customer = customerService.FindCustomer(Convert.ToInt32(txtCustomerId.Text));

        if (customer == null)
        {
            customer = new Customer();
            customer.CreateDate = DateTime.Now;
        }

        customer.Address = txtAddress.Text;
        customer.Comment = txtComment.Text;
        customer.CustomerName = txtCustomerName.Text;
        customer.IsActive = cbIsActive.Checked;
        customer.IsFormal = cbIsFormal.Checked;
        customer.LastUpdate = DateTime.Now;
        customer.Telephone = txtPhone.Text;
        if ( !String.IsNullOrEmpty(txtWarningPrice.Text) )
            customer.WarningPrice =  Convert.ToSingle( txtWarningPrice.Text );

        customerService.Persist(customer);
        GetCustomers(0, true);
    }

    protected void gvCustomers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvCustomers.DataKeys[e.RowIndex].Value);
        Customer customer = customerService.FindCustomer(id);
        customer.IsDeleted = true;
        customerService.Persist(customer);
        GetCustomers(0, true);
    }

    protected void btnClear_ServerClick(object sender, EventArgs e)
    {
        txtCustomerId.Text = "";
        txtAddress.Text = "";
        txtComment.Text = "";
        txtCustomerName.Text = "";
        cbIsActive.Checked = false;
        cbIsFormal.Checked = false;
        txtPhone.Text = "";
        txtWarningPrice.Text = "";
        txtLastUpdate.Text = "";
        txtCreateDate.Text = "";
    }
    protected void gvCustomers_Sorting(object sender, GridViewSortEventArgs e)
    {
        bool dir;
        if ((numberDiv % 2) == 0)
            dir = true;
        else
            dir = false;
        numberDiv++;
        gvCustomers.DataSource = customerService.SortCustomer(e.SortExpression, dir);
        gvCustomers.DataBind();
    }
}
